<?php

namespace app\admin\controller;

use think\facade\App;

class AuthGroup extends Base {

    /**
     * @desc 角色管理
     */
    public function index() {
        $params['keyword'] = input('keyword');

        $logic = App::model('AuthGroup', 'logic');
        $data = $logic->index($params);
        $this->assign($data);

        return view();
    }

    /**
     * @desc 设置权限
     */
    public function authorize() {
        $params['action'] = input('action');
        $params['id']     = input('id');

        $logic = App::model('AuthGroup', 'logic');
        $data = $logic->authorize($params);
        $this->assign($data);
        return view();
    }

    /**
     * @desc 设置权限
     */
    public function authorizeAjax() {
        $params['menuId'] = input('menuId/a');
        $params['id']     = input('id');

        $logic = model('AuthGroup', 'logic');
        $data = [];
        try {
            $data = $logic->authorizeAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }

    /**
     * @desc 添加角色
     */
    public function add() {
        $params['action'] = input('action');

        $logic = App::model('AuthGroup', 'logic');
        $data = $logic->add($params);
        $this->assign($data);

        return view();
    }

    /**
     * @desc 编辑角色
     */
    public function edit() {
        $params['action'] = input('action');
        $params['id']     = input('id');

        $logic = App::model('AuthGroup', 'logic');
        $data = $logic->edit($params);

        $this->assign($data);

        return view();
    }

    /**
     * @desc 角色操作 (添加、编辑)
     */
    public function operateAjax() {
        $params['action']      = input('action');
        $params['id']          = input('id', 0);
        $params['name']        = input('name');
        $params['description'] = input('description');
        $params['status']      = input('status');

        $logic = model('AuthGroup', 'logic');
        $data = [];
        try {
            $data = $logic->operateAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }

}
